package webSpider.MyScrapy;


import java.io.IOException;  
import java.io.InputStream;  
import java.io.UnsupportedEncodingException;  
import java.util.ArrayList;  
import java.util.List;  
import java.util.concurrent.TimeUnit;  
  
import org.apache.http.Header;  
import org.apache.http.HttpEntity;  
import org.apache.http.HttpResponse;  
import org.apache.http.NameValuePair;  
import org.apache.http.client.ClientProtocolException;  
import org.apache.http.client.HttpClient;  
import org.apache.http.client.entity.UrlEncodedFormEntity;  
import org.apache.http.client.methods.HttpGet;  
import org.apache.http.client.methods.HttpPost;  
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;  
import org.htmlcleaner.HtmlCleaner;  
import org.htmlcleaner.TagNode;  
import org.htmlcleaner.XPatherException;  
  
  
/** 
 * ��½ ��ȡcookie 
 *  
 * @author mingyuan 
 *  
 */  
public class Login extends RequestCommons {  
    /** 
     * ��ȡ��½��������Ҫ������ֵ����һ���Ǳ��ύ��ַ���ڶ������������������֡���������vk��ֵ 
     *  
     * @return ���ص�½������string���飬�����Ԫ�أ���һ���Ǳ��ύ��ַ���ڶ������������������֡���������vk��ֵ 
     */  
    private String[] getLoginParameters() {  
        HttpClient httpClient = getHttpClient();  
        String location = "http://3g.sina.com.cn/prog/wapsite/sso/login.php?backURL=http%3A%2F%2Fweibo.cn%2F&backTitle=%D0%C2%C0%CB%CE%A2%B2%A9&vt=4&revalid=2&ns=1";  
        HttpGet get = new HttpGet(location);  
        setHeader(get);  
        HttpResponse response;  
        InputStream content;  
        String retAction = null;  
        String retPassword = null;  
        String retVk = null;  
        try {  
            response = httpClient.execute(get);  
            HttpEntity entity = response.getEntity();  
            content = entity.getContent();  
  
            // ��ȡ��½����  
            HtmlCleaner cleaner = new HtmlCleaner();  
            TagNode tagNode = cleaner.clean(content, "utf-8");  
            Object[] action = tagNode.evaluateXPath("//form/@action");  
  
            if (action.length > 0) {  
                retAction = action[0].toString();  
            }  
            Object[] passwordKey = tagNode.evaluateXPath("//form//input[@type='password']/@name");  
  
            if (passwordKey.length > 0) {  
                retPassword = passwordKey[0].toString();  
            }  
            Object[] vkKey = tagNode.evaluateXPath("//form//input[@name='vk']/@value");  
  
            if (vkKey.length > 0) {  
                retVk = vkKey[0].toString();  
            }  
            EntityUtils.consume(entity);  
        } catch (ClientProtocolException e) {  
            System.out.println("��ȡ��½ҳ��ʧ�ܣ�location=" + location);  
            e.printStackTrace();  
        } catch (IOException e) {  
            System.out.println("��ȡҳ��������ʧ��");  
            e.printStackTrace();  
        } catch (XPatherException e) {  
            System.out.println("������½����ʧ��");  
            e.printStackTrace();  
        } finally {  
            if (get != null) {  
                get.releaseConnection();  
            }  
        }  
  
        System.out.println("����ҳ�棺" + location);  
        System.out.println("�ύ��ַ��" + retAction);  
        System.out.println("������������ƣ�" + retPassword);  
        System.out.println("vkֵ��" + retVk);  
  
        return new String[] { retAction, retPassword, retVk };  
    }  
  
    /** 
     * �ύ�˺����룬��ʼ��½ 
     *  
     * @param postAction 
     *            ��½��ַ 
     * @param userNameValue 
     *            ΢����½�˺� 
     * @param passwordValue 
     *            ΢����½���� 
     * @param passwordKey 
     *            ΢����½���name 
     * @param vkValue 
     *            vk��ֵ 
     * @return ����ȡ����cookie����ת��ַ����ϳ�һ��String���顣��һ��Ԫ��Ϊcookie���ڶ���Ԫ��Ϊ��ת��ַ 
     */  
    private String[] submitPassword(String postAction, String userNameValue, String passwordValue, String passwordKey, String vkValue) {  
        HttpClient httpclient = getHttpClient();  
        String url = "http://3g.sina.com.cn/prog/wapsite/sso/" + postAction;  
        System.out.println("��ʼ�ύ�˺����룺" + url);  
        HttpPost post = new HttpPost(url);  
        setHeader(post);  
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();  
        nvps.add(new BasicNameValuePair("mobile", userNameValue));  
        nvps.add(new BasicNameValuePair(passwordKey, passwordValue));  
        nvps.add(new BasicNameValuePair("remember", "on"));  
        nvps.add(new BasicNameValuePair("vk", vkValue));  
        nvps.add(new BasicNameValuePair("backURL", "http://weibo.cn/"));  
        nvps.add(new BasicNameValuePair("backTitle", "����΢��"));  
        nvps.add(new BasicNameValuePair("submit", "��¼"));  
        HttpResponse response;  
        String cookie = null;  
        String location = null;  
        try {  
            post.setEntity(new UrlEncodedFormEntity(nvps));
            response = httpclient.execute(post);  
            HttpEntity entity2 = response.getEntity();  
            Header[] setCookie = response.getHeaders("Set-Cookie");  
  
            if (setCookie != null) {  
                cookie = setCookie[0].getValue();  
                System.out.println("��ȡ��Cookie��" + cookie);  
            }  
            Header[] locations = response.getHeaders("Location");  
            if (locations != null) {  
                location = locations[0].getValue(); 
                System.out.println("��ȡ����ת���ӣ�" + location);  
            }  
            EntityUtils.consume(entity2);  
        } catch (UnsupportedEncodingException e1) {  
            e1.printStackTrace();  
        } catch (ClientProtocolException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            post.releaseConnection();  
        }  
  
        return new String[] { cookie, location };  
    }  
  
    /** 
     * ��ȡ�ض���ҳ������ 
     *  
     * @param redirectUrl 
     *            ��ȡ�ض���ҳ���ַ 
     * @return ��ȡcookie��Ҫ��ת�ĵ�ַ 
     */  
    private String[] getRedirectPageInfo(String redirectUrl) {  
        System.out.println("��ʼ��ȡ��ת����ҳ��");  
        HttpGet get = new HttpGet(redirectUrl);  
        setHeader(get);  
        HttpResponse redirectResponse;  
        String cookie = null;  
        String clickHref = null;  
        try {  
            redirectResponse = httpclient.execute(get);  
            Header[] headers = redirectResponse.getHeaders("Set-Cookie");  
            if (headers != null) {  
                cookie = headers[0].getValue();  
                String[] splits = cookie.split(";");  
  
                for (String str : splits) {  
                    if (str.startsWith("gsid_CTandWM")) {  
                        cookie = str;  
                        break;  
                    }  
                }  
            }  
            HttpEntity entity = redirectResponse.getEntity();  
            InputStream content = entity.getContent();  
            HtmlCleaner cleaner = new HtmlCleaner();  
            TagNode tagNode = cleaner.clean(content, "utf-8");  
            Object[] clickHrefs = tagNode.evaluateXPath("//div/a/@href");  
  
            if (clickHrefs != null) {  
                clickHref = clickHrefs[0].toString();  
                System.out.println("��ȡ����ת���ӵ�ַ��" + clickHref);  
  
            }  
            EntityUtils.consume(entity);  
        } catch (ClientProtocolException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } catch (XPatherException e) {  
            e.printStackTrace();  
        } finally {  
            get.releaseConnection();  
        }  
        return new String[] { cookie, clickHref };  
    }  
  
    /** 
     * ��ת 
     *  
     * @param cookie 
     *            �ϴ�����ȡ����cookie 
     * @param redirectUrl 
     *            ��תurl 
     * @return ������ת��ȡ�õ�cookie 
     */  
    private String doRedirection(String cookie, String redirectUrl) {  
        HttpGet get = new HttpGet(redirectUrl);  
        setHeader(get);  
        get.setHeader("Cookie", cookie);  
        HttpResponse response;  
        try {  
            response = httpclient.execute(get);  
            HttpEntity entity = response.getEntity();  
            Header[] headers2 = response.getHeaders("Set-Cookie");  
            if (headers2 != null) {  
                cookie = headers2[0].getValue();  
                System.out.println("��תҳ��ȡ�ص�cookie��" + cookie);  
                String[] splits = cookie.split(";");  
                for (String str : splits) {  
                    if (str.startsWith("_WEIBO_UID")) {  
                        cookie = str;  
                        break;  
                    }  
                }  
            }  
            EntityUtils.consume(entity);  
        } catch (ClientProtocolException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        return cookie;  
    }  
  
    /** 
     * �ύ�˺����룬��½ 
     *  
     * @param userNameValue 
     *            ΢���˺� 
     * @param passwordValue 
     *            ΢������ 
     * @return ����cookie 
     */  
    public String doLogin(String userNameValue, String passwordValue) {  
        // ��ȡ��½ҳ��Ĳ���  
        String[] loginParameters = getLoginParameters();  
        String postAction = loginParameters[0];  
        String passwordKey = loginParameters[1];  
        String vkValue = loginParameters[2];  
  
        // �ύ�˺����룬��ȡ�ض���ҳ��������cookie  
        String[] cookieRedirectLocation = submitPassword(postAction, userNameValue, passwordValue, passwordKey, vkValue);  
        String cookie = cookieRedirectLocation[0];  
        String redirectUrl = cookieRedirectLocation[1];  
        // ��ȡ�ض���ҳ������  
        String[] redirectInfo = getRedirectPageInfo(redirectUrl);  
        cookie = redirectInfo[0];  
        redirectUrl = redirectInfo[1];  
        System.out.println("׼����ת");  
        try {  
            TimeUnit.SECONDS.sleep(3);  
        } catch (InterruptedException e) {  
            e.printStackTrace();  
        }  
        System.out.println("��ʼ��ת");  
        String cookieOfRedirect = doRedirection(cookie, redirectUrl);  
  
        StringBuffer sb = new StringBuffer(cookie);  
        sb.append(';').append(cookieOfRedirect);  
        System.out.println("��½�ɹ�������cookieΪ��" + sb.toString());  
        return sb.toString();  
    }  
  
    /** 
     * ʹ�����õ��˺š������½ 
     *  
     * @return ���ص�½cookie 
     */  
      
}  
  
